﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace The_LarX
{
    public enum TimeType
    {
        Milliseconds, Frames
    }
    public class TemporizedAction
    {
        public Action Action;
        /// <summary>
        /// Action called only 1 time, when the Action
        /// turns it off
        /// </summary>
        public Action FinalAction;
        public int Time;
        public TimeType TimeType;
        public int StartTime;
        public bool InfiniteTime;

        protected int Timer;

        public bool Finished;

        public bool Paused;

        public TemporizedAction(int Time, Action action, Action finalAction)
            : this(Time, action, finalAction, TimeType.Milliseconds)
        {
        }
        /// <summary>
        /// Infinite Timer Constructor
        /// </summary>
        /// <param name="Time"></param>
        /// <param name="action"></param>
        /// <param name="finalAction"></param>
        public TemporizedAction(Action action, TimeType type, int StartTime)            
        {
            Engine.TemporizedActions.Actions.Add(this);
            this.Action = action;
            this.TimeType = type;
            this.InfiniteTime = true;
            this.StartTime = StartTime;
        }
        public TemporizedAction(int Time, Action action, Action finalAction, TimeType type)
        {
            Engine.TemporizedActions.Actions.Add(this);
            this.Time = Time;
            this.Action = action;
            this.FinalAction = finalAction;
            this.TimeType = type;
        }

        public void Pause()
        {
            Paused = true;
        }
        public void Play()
        {
            Paused = false;
        }

        public void Reset()
        {
            this.Timer = 0;
        }

        public void Update()
        {
            if (!Paused)
            {
                switch (TimeType)
                {
                    case The_LarX.TimeType.Milliseconds:
                        Timer += Engine.GameTime.ElapsedGameTime.Milliseconds;
                        break;
                    case The_LarX.TimeType.Frames:
                        Timer++;
                        break;
                }

                if (Timer > StartTime)
                {
                    if (Timer > Time)
                    {
                        if (!InfiniteTime)
                        {
                            End();
                        }
                        else
                        {
                            Action();
                        }
                    }
                    else
                    {
                        Action();
                    }
                }
            }
        }
        public void End()
        {
            Engine.TemporizedActions.Actions.Remove(this);
            Finished = true;
            if (FinalAction != null)
                FinalAction();
        }
    }
}
